﻿Shader "SupGames/Mobile/UnityPostProcess"
{
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "" {}
	}

	CGINCLUDE

#include "UnityCG.cginc"
#include "MobileUnityPostProcessUtil.cginc"

	struct appdata {
		fixed4 pos : POSITION;
		fixed2 uv : TEXCOORD0;
	};

	struct v2fb {
		fixed4 pos : POSITION;
		fixed4 uv : TEXCOORD0;
	};
	struct v2f {
		fixed4 pos : POSITION;
		fixed4 uv  : TEXCOORD0;
		fixed2 data  : TEXCOORD1;
	};


	sampler2D _MainTex;
	sampler2D _LutTex2D;
	sampler3D _LutTex3D;
	sampler2D _MaskTex;
	sampler2D _BlurTex;
	fixed _LutAmount;
	fixed _BloomThreshold;
	fixed4 _Color;
	fixed _BloomAmount;
	fixed _BlurAmount;
	fixed _LutDimension;
	fixed _Contrast;
	fixed _Brightness;
	fixed _Saturation;
	fixed _Exposure;
	fixed _Gamma;
	fixed _Offset;
	fixed _Vignette;
	fixed4 _MainTex_TexelSize;

	//
	float4 _Threshold;
	sampler2D _BloomTex;
	 

	v2fb vertBlur(appdata i)
	{
		v2fb o;
		o.pos = UnityObjectToClipPos(i.pos);
		fixed2 offset = _MainTex_TexelSize.xy * _BlurAmount;
		o.uv = fixed4(i.uv - offset, i.uv + offset);
		return o;
	}


	v2f vert(appdata i)
	{
		v2f o;
		o.pos = UnityObjectToClipPos(i.pos);
		o.uv.xy = i.uv;
		o.uv.zw = i.uv - 0.5h;
		o.data.x = i.uv.x - _Offset * _MainTex_TexelSize.x;
		o.data.y = i.uv.x + _Offset * _MainTex_TexelSize.x;
		return o;
	}



	fixed4 fragBloom(v2fb i) : COLOR
	{
		fixed4 result = tex2D(_MainTex, i.uv.xy);

		/*result += tex2D(_MainTex, i.uv.xw);
		result += tex2D(_MainTex, i.uv.zy);
		result += tex2D(_MainTex, i.uv.zw);
		result = QuadraticThreshold(result*0.25h, _Threshold.x, _Threshold.yzw);*/

		
		result = QuadraticThreshold(result, _Threshold.x, _Threshold.yzw);
		return result;

		//return max(result * 0.25h - _BloomThreshold, 0.0h);*/
		//return max(result - _BloomThreshold, 0.0h);
	}



	half4 Combine(half4 bloom, float2 uv)
	{
		//half4 color = SAMPLE_TEXTURE2D(_BloomTex, sampler_BloomTex, uv);
		half4 color = tex2D(_BloomTex, uv);
		return bloom + color;
	}

	fixed4 fragBlur(v2fb i) : COLOR
	{

		fixed4 result =  DownsampleBox4Tap(_MainTex,i.uv,_MainTex_TexelSize);
		return Combine(result, i.uv.xy);
	}

	fixed4 fragAll2D(v2f i) : COLOR
	{
		fixed4 c = tex2D(_MainTex, i.uv);
	    fixed4 b = tex2D(_BlurTex, i.uv);
		c = (c + b * _BloomAmount * _Color);
		return c;
	}

	////////////////////
	half4 Prefilter(half4 color, float2 uv)
	{
		
		color = QuadraticThreshold(color, _Threshold.x, _Threshold.yzw);
		
		return color;
	}
	half4 FragPrefilter4(v2fb i) : SV_Target
	{
		half4 color = DownsampleBox4Tap(_MainTex, i.uv, _MainTex_TexelSize.xy);
		return Prefilter(color, i.uv);
	}

	half4 FragDownsample4(v2fb i) : SV_Target
	{
		half4 color = DownsampleBox4Tap(_MainTex, i.uv, _MainTex_TexelSize.xy);
		return color;
	}

	half4 FragUpsampleBox(v2fb i) : SV_Target
	{
		//half4 bloom = UpsampleBox(_MainTex, i.uv, _MainTex_TexelSize.xy, _SampleScale);
		half4 bloom = UpsampleBox(_MainTex, i.uv, _MainTex_TexelSize.xy, 1);
		return Combine(bloom, i.uv);
	}


	ENDCG


	Subshader
	{
		Pass //0
		{
		  ZTest Always Cull Off ZWrite Off
		  Fog { Mode off }
		  CGPROGRAM
		  #pragma vertex vert
		  #pragma fragment fragAll2D
		  #pragma fragmentoption ARB_precision_hint_fastest
		  #pragma shader_feature BLOOM
		  #pragma shader_feature BLUR
		  #pragma shader_feature CHROMA
		  #pragma shader_feature LUT
		  #pragma shader_feature FILTER
		  ENDCG
		}

		
		Pass //1
		{
		  ZTest Always Cull Off ZWrite Off
		  Fog { Mode off }
		  CGPROGRAM
		  #pragma vertex vertBlur
		  #pragma fragment FragPrefilter4
		  #pragma fragmentoption ARB_precision_hint_fastest
		  ENDCG
		}

		
		
		Pass //2
		{
		  ZTest Always Cull Off ZWrite Off
		  Fog { Mode off }
		  CGPROGRAM
		  #pragma vertex vertBlur
		  #pragma fragment fragBlur
		  #pragma shader_feature BLOOM
		  #pragma fragmentoption ARB_precision_hint_fastest
		  ENDCG
		}

		Pass //3
		{
		  ZTest Always Cull Off ZWrite Off
		  Fog { Mode off }
		  CGPROGRAM
		  #pragma vertex vertBlur
		  #pragma fragment FragDownsample4
		  //#pragma fragmentoption ARB_precision_hint_fastest
		  ENDCG
		}

		Pass //4
		{
		  ZTest Always Cull Off ZWrite Off
		  Fog { Mode off }
		  CGPROGRAM
		  #pragma vertex vertBlur
		  #pragma fragment fragBlur
		  #pragma fragmentoption ARB_precision_hint_fastest
		  ENDCG
		}

		Pass //5
		{
		  ZTest Always Cull Off ZWrite Off
		  Fog { Mode off }
		  CGPROGRAM
		  #pragma vertex vertBlur
		  #pragma fragment FragUpsampleBox
		  #pragma fragmentoption ARB_precision_hint_fastest
		  ENDCG
		}
		
	}
	Fallback off
}